<?php

namespace App\Console\Commands;

use App\Models\Bus\Bus;
use App\Models\TwoLevelMainte\TwoLevelMainte;
use App\Models\TwoLevelMainte\TwoLevelMainteBus;
use App\Models\TwoLevelMainte\TwoLevelMainteCopy;
use Illuminate\Console\Command;

class rebuildTwoLevelData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'rebuild:two';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $twoData = TwoLevelMainteCopy::query()->get()->toArray();
        $bus = Bus::query()->pluck('license_plate_num', 'id')->toArray();
        foreach ($twoData as $k => $item) {
            $typeData = json_decode($item['type'], true);
            $busNum = str_replace('陕G', '', $bus[$item['bus_id']]);
            foreach ($typeData as $kk => $vv) {
                $data = [
                    'type' => $vv['name'],
                    'date' =>$vv['time']
                ];
                $two = new TwoLevelMainte($data);
                $two->save();
                $busData = new TwoLevelMainteBus(['license_plate_num' => $busNum]);
                $two->bus()->save($busData);
            }
        }
    }
}
